import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'Home',
      component: () => import('../views/page/page.vue'),
      meta: { title: '首页' }
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/login.vue'),
      meta: { title: '登录' }
    },
    {
      path: '/test',
      name: 'Test',
      component: () => import('../views/page/test.vue'),
      meta: { title: '心理测试' }
    },
    {
      path: '/questionaire/:id',
      name: 'QuestionaireDetail',
      component: () => import('../views/page/test-detail.vue'),
      meta: { title: '问卷详情' }
    },
    {
      path: '/questionnaire/:id/answer',
      name: 'QuestionnaireAnswer',
      component: () => import('../views/page/test-answer.vue'),
      meta: { title: '问卷答题' }
    },
    {
      path: '/appointment',
      name: 'Appointment',
      component: () => import('../views/page/appointment.vue')
    },
    {
      path: '/announcement',
      name: 'Announcement',
      component: () => import('../views/page/announcement.vue')
    },
    {
      path: '/announcement/:id',
      name: 'AnnouncementDetail',
      component: () => import('../views/page/announcement-detail.vue')
    },
    {
      path: '/messageboard',
      name: 'MessageBoard',
      component: () => import('../views/page/messageboard.vue')
    },
    {
      path: '/doctor',
      name: 'DoctorCenter',
      component: () => import('../views/doctor_page/doctor_page_home.vue'),
      children: [
        {
          path: 'dashboard',
          name: 'DoctorDashboard',
          component: () => import('../views/doctor_page/doctor_dashboard.vue')
        },
        {
          path: 'profile',
          name: 'DoctorProfile',
          component: () => import('../views/doctor_page/doctor_setmessage.vue')
        },
        {
          path: 'appointments',
          name: 'DoctorAppointments',
          component: () => import('../views/doctor_page/doctor_appointment_management.vue')
        },
        {
          path: 'history',
          name: 'DoctorHistory',
          component: () => import('../views/doctor_page/doctor_consulting_history.vue')
        },
        {
          path: 'schedule',
          name: 'DoctorSchedule',
          component: () => import('../views/doctor_page/set_consulting_time.vue')
        },
        {
          path: 'articles',
          name: 'DoctorArticles',
          component: () => import('../views/doctor_page/release_information.vue')
        },
        {
          path: '',
          redirect: '/doctor/dashboard'
        }
      ]
    },
    {
      path: '/user',
      name: 'UserCenter',
      component: () => import('../views/user_page/user_page_home.vue'),
      children: [
        {
          path: 'dashboard',
          name: 'UserDashboard',
          component: () => import('../views/user_page/dashboard.vue')
        },
        {
          path: 'message',
          name: 'UserMessage',
          component: () => import('../views/user_page/user_message.vue')
        },
        {
          path: 'appointment',
          name: 'AppointmentManagement',
          component: () => import('../views/user_page/appointment_management.vue')
        },
        {
          path: 'history',
          name: 'ConsultingHistory',
          component: () => import('../views/user_page/consulting_history.vue')
        },
        {
          path: 'my-questions',
          name: 'MyQuestions',
          component: () => import('../views/user_page/my_questions.vue')
        },
        {
          path: '',
          redirect: '/user/dashboard'
        }
      ]
    },
    {
      path: '/message-board/:id',
      name: 'Message',
      component: () => import('../views/page/message.vue')
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userRole = localStorage.getItem('userRole')
  const token = localStorage.getItem('token')
  
  // 设置全局状态用于控制Header组件的显示
  if (userRole) {
    window.localStorage.setItem('isLoggedIn', 'true')
    window.localStorage.setItem('currentRole', userRole)
    window.localStorage.setItem('username', localStorage.getItem('username') || '用户')
  } else {
    window.localStorage.removeItem('isLoggedIn')
    window.localStorage.removeItem('currentRole')
    window.localStorage.removeItem('username')
  }

  // 需要登录的路由
  if (to.path.startsWith('/message-board/')) {
    // 心理问答详情页面只需要登录即可访问
    if (!userRole) {
      localStorage.setItem('redirectPath', to.fullPath)
      ElMessage.warning('请先登录')
      next('/login')
      return
    }
  } else if (to.path.startsWith('/user') || to.path.startsWith('/doctor')) {
    // 用户中心和医生中心需要检查角色权限
    if (!userRole) {
      localStorage.setItem('redirectPath', to.fullPath)
      ElMessage.warning('请先登录')
      next('/login')
      return
    }
    
    // 检查用户角色权限
    if (to.path.startsWith('/user') && userRole !== 'user') {
      ElMessage.warning('无权访问用户中心')
      next('/')
      return
    }
    
    if (to.path.startsWith('/doctor') && userRole !== 'doctor') {
      ElMessage.warning('无权访问医生中心')
      next('/')
      return
    }
  }
  
  // 已登录用户访问登录页时的处理
  if (to.path === '/login' && userRole) {
    const redirectPath = localStorage.getItem('redirectPath')
    if (redirectPath) {
      localStorage.removeItem('redirectPath')
      next(redirectPath)
    } else {
      // 如果没有重定向路径，根据角色跳转到对应的首页
      next(userRole === 'doctor' ? '/doctor' : '/user')
    }
    return
  }
  
  // 咨询师不能访问预约页面
  if (to.path === '/appointment' && userRole === 'doctor') {
    ElMessage.warning('咨询师不能预约咨询')
    next(from.path)
    return
  }
  
  next()
})

export default router 